package Service;

import GUI.ChatFrame;
import Utils.ChatManage;
import Utils.ThreadManage;
import org.json.JSONObject;

/**
 * The type Receive msg.
 *
 * @author dzr
 * @version 1.0
 */
public class ReceiveMsg {
    /**
     * The constant MSG_TEXT.
     */
    public static final int MSG_TEXT = 0;

    /**
     * Receive message.
     *
     * @param jsonObject the json object of message
     */
    public static void receiveMsg(JSONObject jsonObject){
        int r_uid = jsonObject.getInt("r_uid");
        int s_uid = jsonObject.getInt("s_uid");
        String s_name = jsonObject.getString("s_name");
        String content = jsonObject.getString("content");
        String s_time = jsonObject.getString("time");
        if(ChatManage.hasChat(s_uid)){
            ChatManage.getChat(s_uid).showMsg(MSG_TEXT, s_uid, s_name, s_time, content);
        }else{
            ChatFrame chatFrame = new ChatFrame(ThreadManage.getInstance().getClientThread(r_uid).getUsername(), r_uid, s_name, s_uid);
            ChatManage.addChat(s_uid, chatFrame);
            chatFrame.showMsg(MSG_TEXT, s_uid, s_name, s_time, content);
        }
    }
}
